<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class account_mod extends module
{
    function __construct()
    {
        $this->uid = @$_SESSION['user']['uid'];
        parent::__construct();
    }
    public function get_trade($trade_no)
    {
        $uid = $this->uid;
        $wsql = '';
        if (!empty($uid)) {
            $wsql = ' uid = ' . $this->uid . " and ";
        }
        $total_price = 0;
        $result = $this->db->get_all("select * from " . table("admin_pay") . " where " . $wsql . " trade_no = '" . $trade_no . "'");
        if ($result) {
            foreach ($result as $v) {
                if ($v['pay_status'] == '0' && $v['pay_time'] == '0') {
                    $pay_status[] = '';
                }
                $oarr[] = $v['order_no'];
                $total_price += $v['pay_price'];
            }
            if (isset($oarr)) {
                if (isset($pay_status)) {
                    $pay_status = '0';
                } else {
                    $pay_status = '1';
                }
                if (isset($oarr)) {
                    $o_in = rtrim(implode(',', $oarr), ',');
                    $oall = $this->db->get_all("select id,order_no,total_price,pay_time from " . table('order_info') . " where order_no in(" . $o_in . ")");
                    foreach ($oall as $v) {
                        $garr[] = $v['id'];
                    }
                    $g_in = implode(',', $garr);
                    $gall = $this->db->get_all("select id,oid,uid,title,price from " . table('order_goods') . " where oid in(" . $g_in . ")");
                    foreach ($gall as $v) {
                        $title_arr[] = $v['title'];
                        $g_list[$v['id']] = $v;
                    }
                    $title = implode('', $title_arr);
                }
                return array('error' => '0', 'trade_no' => $trade_no, 'price' => $total_price, 'title' => cut_str($title, '30'), 'order_list' => $g_list, 'pay_status' => $pay_status);
            } else {
                return array('error' => '1', 'msg' => '交易无订单号');
            }
        } else {
            return array('error' => '1', 'msg' => '订单交易不存在');
        }
    }
    public function get_order($oid)
    {
        return $this->db->get_one("select id,pay_time,order_no,title,total_price from " . table('order_info') . " where order_no = " . $oid);
    }
    public function pay_order($oid)
    {
        $row = $this->db->get_one("select id,pay_time,order_no,title,total_price from " . table('order_info') . " where order_no = " . $oid);
        if ($row['pay_time'] == '0') {
            $trade_no = build_order_no();
            $this->db->autocommit();
            $p_query = $this->db->update(table('admin_pay'), array('trade_no' => $trade_no), 'order_no = ' . $row['order_no']);
            $o_query = $this->db->update(table('order_info'), array('trade_no' => $trade_no), 'id = ' . $row['id']);
            if ($o_query && $p_query) {
                $this->db->commit();
                return array('trade_no' => $trade_no, 'title' => $row['title'], 'pay_price' => $row['total_price']);
            } else {
                $this->db->rollback();
                showmsg('支付错误');
            }
        } else {
            showmsg('已经支付过了');
        }
    }
    public function bill($arr)
    {
        $page = isset($arr['page']) ? $arr['page'] : '1';
        $page_size = isset($arr['page_size']) ? $arr['page_size'] : '20';
        $result = $this->db->get_all("select * from " . table('member_bill') . " where uid = " . $this->uid . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($result) {
            foreach ($result as $k => $v) {
                $v['addtime'] = date('Y.m.d H:i', $v['addtime']);
                $list[] = $v;
            }
            $data['list'] = @$list;
            $total = $this->db->get_one("select count(id) as total from " . table('member_bill') . " where uid = " . $this->uid);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            return $data;
        } else {
            return false;
        }
    }
    public function withdraw_one($id)
    {
        $result = $this->db->get_one("select * from " . table('member_withdraw') . " where uid = " . $this->uid . " and id = " . $id);
        if ($result['id']) {
            return $result;
        } else {
            return false;
        }
    }
    public function withdraw_list($arr)
    {
        $page = isset($arr['page']) ? $arr['page'] : '1';
        $page_size = isset($arr['page_size']) ? $arr['page_size'] : '20';
        $result = $this->db->get_all("select * from " . table('member_withdraw') . " where uid = " . $this->uid . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($result) {
            foreach ($result as $k => $v) {
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(*) as total from " . table('member_withdraw') . " where uid = " . $this->uid);
            if ($page_size < $total['page']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            return $data;
        }
    }
    public function withdraw_apply($id = null)
    {
        $userRow = $this->db->get_one("select uid,price from " . table('member') . " where uid = " . $this->uid);
        $setsql['pay_type'] = !empty($_POST['pay_type']) ? $_POST['pay_type'] : showmsg('支付类型不能为空');
        $setsql['pay_name'] = !empty($_POST['pay_name']) ? $_POST['pay_name'] : showmsg('支付姓名不能为空');
        $setsql['pay_account'] = !empty($_POST['pay_account']) ? $_POST['pay_account'] : showmsg('支付姓名不能为空');
        $setsql['pay_remark'] = isset($_POST['pay_remark']) ? $_POST['pay_remark'] : '';
        $setsql['pay_status'] = '0';
        $setsql['addtime'] = time();
        if ($id) {
            $row = $this->db->get_one("select * from " . table("member_withdraw") . " where uid = " . $this->uid . " and id = " . $id);
            if ($row['id'] && $row['pay_status'] == '2') {
                $query = $this->db->update(table('member_withdraw'), $setsql, 'uid = ' . $this->uid . ' and id = ' . $id);
                if ($query) {
                    showmsg('申请成功');
                } else {
                    showmsg('申请失败');
                }
            } else {
                showmsg('已经提交了申请，无法修改');
            }
        } else {
            $price = !empty($_POST['price']) ? $_POST['price'] : showmsg('提现金额不能为空');
            $setsql['price'] = $price;
            $setsql['surplus'] = $row['price'] - $price;
            $setsql['uid'] = $this->uid;
            if ($price > '0') {
                if ($userRow['price'] - $price >= '0') {
                    $addQuery = $this->db->add(table('member_withdraw'), $setsql);
                    $subQuery = $this->db->query("UPDATE " . table('member') . " SET price = price - " . $price . " WHERE `uid` =" . $this->uid);
                    if ($addQuery && $subQuery) {
                        $this->db->commit();
                        showmsg('申请成功,等待审核发放');
                    } else {
                        $this->db->rollback();
                        showmsg('申请失败');
                    }
                } else {
                    showmsg('账户余额不足');
                }
            } else {
                showmsg('提取金额不能小于0');
            }
        }
    }
    function add_bill($uid, $name, $price = '0', $pay_io = '1', $pay_type = '1', $pay_no = null, $trade_no = null, $is_pay = false, $pay_remark = null)
    {
        $arr['uid'] = $uid;
        $arr['name'] = $name;
        $arr['price'] = $price;
        $arr['type'] = $pay_type;
        $arr['order_no'] = $pay_no;
        $arr['status'] = '1';
        $arr['remark'] = empty($pay_remark) ? $pay_remark : '';
        $arr['addtime'] = time();
        $query = $this->db->add(table('member_bill'), $arr);
        if ($query) {
            return true;
        } else {
            return false;
        }
    }
    function add($uid, $price, $name, $type = '0', $order_no = null, $remark = null)
    {
        $arr['uid'] = $uid;
        $arr['name'] = $name;
        $arr['price'] = $price;
        $arr['type'] = $type;
        $arr['order_no'] = $order_no;
        $arr['status'] = '1';
        $arr['remark'] = $remark;
        $arr['addtime'] = time();
        $q1 = $this->db->add(table('member_bill'), $arr);
        $q2 = $this->db->query("update " . table('member') . " set price = price + " . $price . " where uid = " . $uid);
        if ($q1 && $q2) {
            return true;
        } else {
            return false;
        }
    }
    function reduce($uid, $price, $name, $type = '0', $order_no = null, $remark = null)
    {
        $this->db->autocommit();
        $arr['uid'] = $uid;
        $arr['name'] = $name;
        $arr['price'] = '-' . $price;
        $arr['type'] = $type;
        $arr['order_no'] = $order_no;
        $arr['status'] = '1';
        $arr['remark'] = $remark;
        $arr['addtime'] = time();
        $q1 = $this->db->add(table('member_bill'), $arr);
        $q2 = $this->db->query("update " . table('member') . " set price = price - " . $price . " where uid = " . $uid);
        if ($q1 && $q2) {
            $this->db->rollback();
            return true;
        } else {
            $this->db->commit();
            return false;
        }
    }
    function pay_type()
    {
        return array('1' => '购物支出', '2' => '充值订单', '3' => '商户收入', '4' => '维权退款', '5' => '维权其他', '6' => '拼团支付');
    }
}